import React from "react"
import { inject, observer } from 'mobx-react';
import {WeaTools,WeaErrorPage,WeaRightMenu,WeaTop,WeaAlertPage} from "ecCom"
import {Button} from "antd"
import {toJS} from "mobx"
import WeaCptTableEdit from '../CapitalTableEdit/WeaCptTableEdit'
import equals from "deep-equal"
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('cptManagerStore')
@observer
class CptManager extends React.Component{
    constructor(props){
        super(props);
    }
    componentDidMount(){
        const {params,cptManagerStore} = this.props;
        const {initTableInfo} = cptManagerStore;
        initTableInfo({viewtype:params.viewtype});
    }
    componentWillReceiveProps(nextProps){
        const keyOld = this.props.location.key;
		const keyNew = nextProps.location.key;
		const paramsOld = this.props.params;
        const paramsNew = nextProps.params;
        if(keyOld!==keyNew||paramsOld!==paramsNew) {
			const {params,cptManagerStore} = nextProps;
            const {initTableInfo} = cptManagerStore;	
            initTableInfo({viewtype:params.viewtype});
        }
            
    }
    componentWillUnmount(){
        const {cptManagerStore} = this.props;
        cptManagerStore.clearStatus();
    }
    render(){
        const {cptManagerStore} = this.props;
        const {loading,tableInfo,submitDatas,hasRight,verified,viewtype,batchBrowser} = cptManagerStore;
        let title = getLabel(886,"资产领用");
        if("cptuse"==viewtype){
            title = getLabel(886,"资产领用");           
        }else if("cptmove"==viewtype){
            title = getLabel(883,"资产调拨"); 
        }else if("cptlend"==viewtype){
            title = getLabel(6051,"资产借用"); 
        }else if("cptloss"==viewtype){
            title = getLabel(6054,"资产减损"); 
        }else if("cptdiscard"==viewtype){
            title = getLabel(6052,"资产报废"); 
        }else if("cptmend"==viewtype){
            title = getLabel(22459,"资产送修"); 
        }else if("cptback"==viewtype){
            title = getLabel(15305,"资产归还"); 
        }else if("cptchange"==viewtype){
            title = getLabel(6055,"资产变更"); 
        };
        if (verified && !hasRight) {
            return (<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@vuikso`} >
                    <div style={{color : '#000'}}>
                        {getLabel(2012,'对不起，您暂时没有权限！')}
                    </div>
                </WeaAlertPage>
            )
        }
        if (verified && hasRight) {
            return (<div className='wea-capital-search'>
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@x6eul2`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}>
                        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@74grmi`}
                            title={title}
                            loading={loading}
                            icon={<i className='icon-coms-fa' />}
                            iconBgcolor='#f14a2d'
                            buttons={this.getButtons()}
                            buttonSpace={10}
                            showDropIcon={true}
                            dropMenuDatas={this.getRightMenu()}
                            onDropMenuClick={this.onRightMenuClick}
                        >
                    <WeaCptTableEdit ecId={`${this && this.props && this.props.ecId || ''}_WeaCptTableEdit@dh1y7g`}
                            ref = { ref => { cptManagerStore.remind = ref }}
                            showGroup={true}
                            needAdd={true}
                            needCopy={true}
                            columns={toJS(tableInfo.columnData) || []}
                            datas={toJS(submitDatas)}
                            datas_0={toJS(tableInfo.columnDefine)||[]}
                            onChange={this.editTableDatas.bind(this)}
                            batchBrowser={toJS(batchBrowser)}
                        />
                        </WeaTop>
                    </WeaRightMenu>
                </div>)
        }
        return (<div></div>)
    }
    getButtons() {
        const {cptManagerStore} = this.props;
        const {visible} = cptManagerStore;
        let btnArr = [];
		btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gwkjss@submit`} type="primary" disabled={visible} onClick={()=>{cptManagerStore.doSubmit()}}>{getLabel(615,"提交")}</Button>)
        return btnArr
    }
    getRightMenu(){
        const {cptManagerStore} = this.props;
        const {visible} = cptManagerStore;
        let btns = [];
		btns.push({
			icon: <i className='icon-coms-Preservation'/>,
            content:getLabel(615,"提交"),
            disabled:visible,
            key:0
           
		});
    	return btns
    }
	onRightMenuClick = (key) => {
		const {cptManagerStore} = this.props;
		if(Number(key)==0){
			cptManagerStore.doSubmit();
		}
    }
    editTableDatas=(datas)=>{
        const {cptManagerStore} = this.props;
        cptManagerStore.saveTableInfoDatas(datas);
    }
}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@y4zq0v`} msg={ props.error ? props.error : getLabel( 383324,'对不起，该页面异常，请联系管理员' )} />,
    {error: ""}
)(CptManager);